package gui.display;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import utils.Chat;
import utils.IRCParser;

/**
 *
 */
public class ChatDisplay extends Display
{
    private final Chat chat_;
    private ChannelUsersPanel usersPanel_;
    private ChannelTopicPanel topicPanel_;

    public ChatDisplay( Chat chat )
    {
        super();
        //add( _usersPanel, BorderLayout.EAST );
        chat_ = chat;
        inputField_.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
                JTextField source = (JTextField) e.getSource();
                chat_.handleInput( source.getText() );
                source.setText( null );
            }
        });
    }

    public final void printPRIVMSG( String author, String message )
    {
        printTimeStamp();
        printStyledText( "<" + author + ">",
                author.equalsIgnoreCase( chat_.getCurrentNick() ) ?
                BLUEBOLD
                : BLUE );
        printStyledText( " " + message, REGULAR );
        printNewLine();
    }

    public final void printEmote( String author, String message )
    {
        printTimeStamp();
        printStyledText( "***" + author, BLUEBOLD );
        printStyledText( " " + message, REGULAR );
        printNewLine();
    }

    public final void print( String output )
    {
        if( IRCParser.isCommandMessage( output ) )
        {
            String command = IRCParser.getCommand( output );
            String author = IRCParser.getAuthor( output );

            if( command.equalsIgnoreCase( "PRIVMSG" ) )
            {
                String message = IRCParser.getMessage( output );

                if( message.startsWith( "\001ACTION" ) )
                {
                    message = message.substring( 8 ).trim();
                    printEmote( author, message );
                }
                else
                    printPRIVMSG( author, message );
                return;
            }
            else if( command.equalsIgnoreCase( "NICK" ) )
            {
                String newNick = IRCParser.getMessage( output );
                printTimeStamp();
                printStyledText( author + " is now known as " + newNick, BOLD );
                printNewLine();
                return;
            }
            else if( command.equalsIgnoreCase( "JOIN" ) )
            {
                printTimeStamp();
                printStyledText( author + " [ " + IRCParser.getAuthorHost( output ) + "] entered the room.", BOLD );
                printNewLine();
                return;
            }
            else if( command.equalsIgnoreCase( "PART" ) )
            {
                String message = IRCParser.getMessage( output );
                printTimeStamp();
                printStyledText( author + " left the room (part: " + message + ").", BOLD );
                printNewLine();
                return;
            }
            else if( command.equalsIgnoreCase( "QUIT" ) )
            {
                String message = IRCParser.getMessage( output );
                printTimeStamp();
                printStyledText( author + " left the room (quit: " + message + ").", BOLD );
                printNewLine();
                return;
            }
            else if( command.equalsIgnoreCase( "MODE" ) )
            {
                String message = IRCParser.getMessage( output );
                printTimeStamp();
                printStyledText( "mode (" + message + ") by " + author, BOLD );
                printNewLine();
                return;
            }
            else if( command.equalsIgnoreCase( "TOPIC" ) )
            {
                String newTopic = IRCParser.getMessage( output );
                if( topicPanel_ == null )
                {
                    topicPanel_ = new ChannelTopicPanel();
                    add( topicPanel_, BorderLayout.NORTH );
                    validate();
                }
                topicPanel_.setTopic( newTopic );
                printTimeStamp();
                printStyledText( author + " changed the topic to: " + newTopic, BOLD );
                printNewLine();
                return;
            }
        }
        else
        {
            final int status = IRCParser.getStatusCode( output );
            switch( status )
            {
                case IRCParser.RPL_TOPIC:
                    // Handle TOPIC
                    if( topicPanel_ == null )
                    {
                        topicPanel_ = new ChannelTopicPanel();
                        add( topicPanel_, BorderLayout.NORTH );
                        validate();
                        repaint();
                    }

                    topicPanel_.setTopic( IRCParser.getMessage( output ) );
                    return;
                case IRCParser.RPL_TOPICWHOTIME:
                    // TODO?
                    return;
                case IRCParser.RPL_NAMREPLY:
                    if( usersPanel_ == null )
                    {
                        usersPanel_ = new ChannelUsersPanel( this );
                        add( usersPanel_, BorderLayout.EAST );
                    }
                    else
                        usersPanel_.reset();
                    usersPanel_.addUsers( IRCParser.getMessage( output) );
                    validate();
                    repaint();
                    return;
                case IRCParser.RPL_ENDOFNAMES:
                    // Nothing to do
                    return;
                default:
                    break;
            }
        }

        printStyledText( output, REGULAR );
        printNewLine();
    }

    public final void userPrint( String message )
    {
        printPRIVMSG( chat_.getCurrentNick(), message );
    }
}
